#!/bin/sh
echo 
echo "<title>boot-config</title>"
echo "<table align=center border=0><tr><td>"
btc=$(cat /tmp/btc)
if [ "$btc" != "55" ]; then echo `cat /www/menu.html`; else
echo "<td><a href=/cgi-bin/luci/admin/system/bootconfig><button class="button" type=submit title="$SERVER_NAME'/cgi-bin/luci'">"Exit setting without applying changes."</button></a></td>"
fi
echo "</td></tr></table>"

echo "
<body bgcolor="#f0f0f0">
<style>
.button {
 text-decoration:none; text-align:center; 
 padding:5px 10px; 
 border:solid 1px #004F72; 
 -webkit-border-radius:20px;
 -moz-border-radius:20px; 
 border-radius: 20px; 
 font:15px Arial, Helvetica, sans-serif; 
 font-weight:bold; 
 color:#ffffff; 
 background-color:#3BA4C7; 
 background-image: -moz-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -webkit-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -o-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -ms-linear-gradient(top, #3BA4C7 0% ,#1982A5 100%); 
 filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1982A5', endColorstr='#1982A5',GradientType=0 ); 
 background-image: linear-gradient(top, #3BA4C7 0% ,#1982A5 100%);   
 -webkit-box-shadow:0px 0px 2px #bababa, inset 0px 0px 1px #ffffff; 
 -moz-box-shadow: 0px 0px 2px #bababa,  inset 0px 0px 1px #ffffff;  
 box-shadow:0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;  
  
  }.button:hover{
 padding:5px 10px; 
 border:solid 1px #004F72; 
 -webkit-border-radius:20px;
 -moz-border-radius:20px; 
 border-radius: 20px; 
 font:15px Arial, Helvetica, sans-serif; 
 font-weight:bold; 
 color:#000000; 
 background-color:#3BA4C7; 
 background-image: -moz-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -webkit-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -o-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -ms-linear-gradient(top, #3BA4C7 0% ,#1982A5 100%); 
 filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1982A5', endColorstr='#1982A5',GradientType=0 ); 
 background-image: linear-gradient(top, #3BA4C7 0% ,#1982A5 100%);   
 -webkit-box-shadow:0px 0px 2px #bababa, inset 0px 0px 1px #ffffff; 
 -moz-box-shadow: 0px 0px 2px #bababa,  inset 0px 0px 1px #ffffff;  
 box-shadow:0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;  
  
 }.button:active{
 padding:5px 10px; 
 border:solid 1px #004F72; 
 -webkit-border-radius:10px;
 -moz-border-radius:10px; 
 border-radius: 10px; 
 font:15px Arial, Helvetica, sans-serif; 
 font-weight:bold; 
 color:#ffffff; 
 background-color:#3BA4C7; 
 background-image: -moz-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -webkit-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -o-linear-gradient(top, #3BA4C7 0%, #1982A5 100%); 
 background-image: -ms-linear-gradient(top, #3BA4C7 0% ,#1982A5 100%); 
 filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1982A5', endColorstr='#1982A5',GradientType=0 ); 
 background-image: linear-gradient(top, #3BA4C7 0% ,#1982A5 100%);   
 -webkit-box-shadow:0px 0px 2px #bababa, inset 0px 0px 1px #ffffff; 
 -moz-box-shadow: 0px 0px 2px #bababa,  inset 0px 0px 1px #ffffff;  
 box-shadow:0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;  
  
 }
  .b2 {
height: 10px;
text-align: center;
padding: 1px;
border: 3px solid #0000cc;
border-radius: 10px;
}
</style>
<br>"
#***************************************************************
board=$(cat /tmp/sysinfo/board_name)
boardname="${board##*,}"
model=$(cat /proc/device-tree/model)
boardmodel="${model}"
backup='/boot/dtb.backup'
dts='/tmp/t-dts'
wifi='mmc@1c10000\|mmc@01c10000'
mmc1='mmc@1c10000\|mmc@01c10000'
w1='w1'
i2c4='i2c4'
ir='ir@1f02000\|ir@01f02000'
pwm='pwm@1c21400\|pwm@01c21400'
i2c0='i2c@1c2ac00\|i2c@01c2ac00'
i2c1='i2c@1c2b000\|i2c@01c2b000'
i2c2='i2c@1c2b400\|i2c@01c2b400'
audcodec='codec@1c22c00\|codec@01c22c00'
uart0='serial@1c28000\|serial@01c28000'
uart1='serial@1c28400\|serial@01c28400'
uart2='serial@1c28800\|serial@01c28800'
otg_reg='usb@1c19000\|usb@01c19000'
ehci0='usb@1c1a000\|usb@01c1a000'
ohci0='usb@1c1a400\|usb@01c1a400'
ehci1='usb@1c1b000\|usb@01c1b000'
ohci1='usb@1c1b400\|usb@01c1b400'
ehci2='usb@1c1c000\|usb@01c1c000'
ohci2='usb@1c1c400\|usb@01c1c400'
ehci3='usb@1c1d000\|usb@01c1d000'
ohci3='usb@1c1d400\|usb@01c1d400'
spi0='spi@1c68000\|spi@01c68000'
spi1='spi@1c69000\|spi@01c69000'
ethernet0='ethernet@1c30000\|ethernet@01c30000'
spdif='spdif@1c21000\|spdif@01c21000'
gpio_keys_user='gpio_keys_user'
rotary_button='rotary_button'
rotary_axis='rotary_axis'
ledspin=''
i2cn0='i2c0\|i2c0-pins'
i2cn1='i2c1\|i2c1-pins'
i2cn2='i2c2\|i2c2-pins'
uartn0='uart0\|uart0-pins'
uartn1='uart1\|uart1-pins'
uartn2='uart2\|uart2-pins'
spin0='spi0\|spi0-pins'
spin1='spi1\|spi1-pins'
spdifn='spdif\|spdif-tx-pin'
irn='ir\|r-ir-rx-pin'
#***************************************************************

stachec () {
if [ "$mod" == "okay" ]; then
yss="hidden"
noo=""
checked=checked
checked1=""
sost=okay
sost1=disabled
sost2=okay
sinvers=Disable
bgcolor="#76cc00"
else
bgcolor=""
sost2=disabled
sost1=okay
yss=""
noo="hidden"
checked=""
checked1=checked
sost=disabled
sinvers=Enable
fi
}

stausb () {
if [[ "$ehci" == "okay" && "$ohci" == "okay" ]]; then
checusb=checked
checusb1=""
sostusb1=disabled
on_off_usb=off
usbcolor="#76cc00"
sostusb2=okay
else
usbcolor=""
checusb=""
checusb1=checked
sostusb1=okay
on_off_usb=on
sostusb2=disabled
fi
}

hex_convert() {
hexNum_gpios=$(cat $dts | sed -n '/'$modul' {/,/\};/p' | sed -n 's/.'$gpios' = <\(.*\) \(.*\) \(.*\) \(.*\)>.*/\3/p' | sed -e "s/^.*0[xX]//g" | tr '[a-f]' '[A-F]')
num_gpios=$(echo "obase=10; ibase=16; $hexNum_gpios" | bc)
}

select_option() {
hexNum_gpio_bank=$(cat $dts | sed -n '/'$modul' {/,/\};/p' | sed -n 's/.'$gpios' = <\(.*\) \(.*\) \(.*\) \(.*\)>.*/\2/p' | sed -e "s/\\t\| //g")
  if [[ "$hexNum_gpio_bank" == "0" || "$hexNum_gpio_bank" == "0x0" || "$hexNum_gpio_bank" == "0x00" ]]; then gpio_bank="PA";
  elif [[ "$hexNum_gpio_bank" == "0x2" || "$hexNum_gpio_bank" == "0x02" ]]; then gpio_bank="PC";
  elif [[ "$hexNum_gpio_bank" == "0x3" || "$hexNum_gpio_bank" == "0x03" ]]; then gpio_bank="PD";
  elif [[ "$hexNum_gpio_bank" == "0x4" || "$hexNum_gpio_bank" == "0x04" ]]; then gpio_bank="PE";
  elif [[ "$hexNum_gpio_bank" == "0x5" || "$hexNum_gpio_bank" == "0x05" ]]; then gpio_bank="PF";
  elif [[ "$hexNum_gpio_bank" == "0x6" || "$hexNum_gpio_bank" == "0x06" ]]; then gpio_bank="PG";
  elif [[ "$hexNum_gpio_bank" == "0xb" || "$hexNum_gpio_bank" == "0x0b" ]]; then gpio_bank="PL";
  fi
echo "
<select class="button" name="$gpios_modul">
<option>$gpio_bank$num_gpios</option>"
for i in 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 18 19; do
echo "<option>PA$i</option>"
done
echo "
<option>PG6</option>
<option>PG7</option>
<option>PL11</option>
</select>"
}

if [ "$boardname" == "orangepi-r1" ]; then
 powerwifi='Turn on/off the power of the rtl8189es radio module.'
 pownamwifi='Wifi: rtl8189es'
 textpownam='vcc-wifi:'
 usb1='Ethernet1'
 txt_usb1='RTL8152B USB-Ethernet'
 ledspin=' (status_Red-Led)'
 spi1_hide=''
 sdio='sdio_wifi'
elif [ "$boardname" == "orangepi-one" ]; then
 powerwifi='Turn on/off the power of the rtl8189es radio module.'
 pownamwifi='Wifi: rtl8189es'
 textpownam='vcc-wifi:'
 usb1='usb1'
 txt_usb1='---'
 ledspin=''
 spi1_hide='hide'
 sdio='sdio_wifi'
elif [[ "$boardname" == "orangepi-zero" || "$boardname" == "orangepi-zero-lts" ]]; then
 powerwifi='Turn on/off the power of the xr819 radio module.'
 pownamwifi='Wifi: xradio, xr819'
 textpownam='vcc-wifi:'
 usb1='usb1'
 txt_usb1='---'
 ledspin='status_led PA17-Red'
 spi1_hide=''
 sdio='sdio_wifi'
else
 powerwifi='Turn on/off MMC1.'
 pownamwifi='mmc1: '
 textpownam=''
 usb1='usb1'
 txt_usb1='---'
 ledspin=''
 spi1_hide=''
 sdio='mmc1'
fi

if [[ "$1" == "overlays" || "$1" == "save_okay" ]]; then
echo "<table align="center" border=0 width=><tr><td>"
if [ "$1" == "overlays" ]; then
echo "
The <b>$2</b> overlay has been added to the boot configuration file.
</br>And it will be converted to <b>DTB</b> in <b><span id="time"></span></b> seconds.
<script type="text/javascript">var i = 5;"
elif [ "$1" == "save_okay" ]; then
echo "<b><span hidden id="time"></span></b>
<script type="text/javascript">var i = 0;"
fi
echo " function time(){ document.getElementById(\"time\").innerHTML = i; i--; if (i < 0) location.href = "
if [ "$btc" != "55" ]; then
echo "\"/boot/index.html\"; }"
else
echo "\"/cgi-bin/modules/boot-config/luci.htm\"; }"
fi
echo " time(); setInterval(time, 1000);"
echo " </script>
</td></tr></table>"
if [ "$1" == "overlays" ]; then boot-config overlays $2 &> /dev/null; fi
fi

if [ "$1" == "1" ]; then
echo "<table align="center" border=0 width=><tr><td>
The device reboots.  And will be available in <b><span id="time"></span></b> seconds.
<script type="text/javascript">var i = 30; function time(){ document.getElementById(\"time\").innerHTML = i; i--; if (i < 0) location.href = \"/boot/index.html\"; }
time(); setInterval(time, 1000); </script>
</td></tr></table>"
reboot
fi

mtd=$(sed -n 's/:\(.*\)\"dtb\"//p' /proc/mtd)
cat $dts &> /dev/null
if [ $? = 1 ]; then
df -h | grep overlayfs &> /dev/null
if [ $? != 1 ]; then
dtc -I dtb -O dts -o $dts /dev/$mtd
else
dtc -I dtb -O dts -o $dts /boot/dtb
fi
sleep 1
fi

cat $dts &> /dev/null
if [ $? = 1 ]; then
echo "<table class="b2" align="center" border=0 width=><tr><td>"
df -h | grep overlayfs &> /dev/null
if [ $? != 1 ]; then
echo "You must install <b>dtc</b><br>"
else
echo "Not mounted <b>/dev/mmcblk0p1</b> in <b>/boot</b><br>"
echo "You must install <b>mount-utils block-mount dtc</b><br>"
echo "And make changes to the config:<br><br>"
echo "<b>uci set fstab.@mount[0].target=/boot<br>"
echo "uci set fstab.@mount[0].enabled=1<br>"
echo "uci commit fstab</b><br>"
echo "Reboot device.<br>"
fi
echo "</td></tr></table>"
nomount='disabled'
fi

df -h | grep overlayfs &> /dev/null
if [ $? != 1 ]; then
dtc='dtsspi'
dtb='dtbspi'
else
dtc='dtsmmc'
dtb='dtbmmc'
fi
echo "<table align="center" border=0 width=>
<form method="POST" action="conv.cgi"><tr>
<td>
<input name="mtd-dtb" hidden value="$mtd">
<a href=/cgi-bin/modules/boot-config/conv.cgi>
<button class="button" $nomount type=submit name="conv" value="$dtc" title="Device_tree_compile_for_flat_device_trees.">"Convert $dtb to dts-file."</button></a>
</td>
<td>"
df -h | grep overlayfs &> /dev/null
if [ $? = 1 ]; then
cat $backup &> /dev/null
if [ $? = 1 ]; then
echo "<a href=/cgi-bin/modules/boot-config/conv.cgi>
<button class="button" $nomount type=submit name="backup" value="backdtb">"Create a backup of u-boot \(dtb-file\)."</button></a>"
else
echo "<a href=/cgi-bin/modules/boot-config/conv.cgi>
<button class="button" $nomount type=submit name="resback" value="resdtb">"Restore u-boot \(dtb-file\) from backup."</button></a>"
fi
fi
echo "</td>
</tr></form></table>"

#***************************************************************
#*************   CPU-FREQ  **********************
echo "<table align="center" class="b2" bgcolor="#ffffff" cellspacing="2">
<form method="POST" action="conv.cgi">"
cat /etc/rc.d/S30cpufreq_scaling &> /dev/null
if [ $? != 1 ]; then
works='astarok'
astcolor="#76cc00"
else
works='noastar'
astcolor=""
fi
disbl=''
echo "<tr><td bgcolor="$astcolor">"Max cpu-freq: "</td><td>"
case "`cat /sys/bus/cpu/devices/cpu0/cpufreq/scaling_max_freq`" in
"648000" ) maxfreq1=selected; set_cpu_freq="648000";;
"816000" ) maxfreq2=selected; set_cpu_freq="816000";;
"1008000" ) maxfreq3=selected; set_cpu_freq="1008000";;
*) maxfreq4=selected; disbl="disabled";;
esac
echo "<select $disbl class="button" name="max_freq">
<option $maxfreq1>"648MHz"</option>
<option $maxfreq2>"816MHz"</option>
<option $maxfreq3>"1008MHz"</option>
<option disabled $maxfreq4>"Not supported."</option>
</select>
<input name="astart" hidden value="$works">
<button class="button" $disbl type=submit name="set_cpu_freq" value="$set_cpu_freq">"Set cpu-freq"</button>"
echo "</td></tr>"

#*************  CPU Cores  **********************
dsbl=''
echo "<tr><td bgcolor="$astcolor">"CPU Cores: "</td><td>"
case "`uci get cpufreq_scaling.@cpufreq[0].corekeeper`" in
"1" ) corkeep0=selected; set_core_keeper="1";;
"2" ) corkeep1=selected; set_core_keeper="2";;
"4" ) corkeep3=selected; set_core_keeper="4";;
*) corkeep4=selected; dsbl="disabled";;
esac
echo "<select $disbl class="button" name="corekeeper">
<option $corkeep0>"1 cores"</option>
<option $corkeep1>"2 cores"</option>
<option $corkeep3>"4 cores"</option>
<option disabled $corkeep4>"Not supported."</option>
</select>
<input name="astart" hidden value="$works">
<button class="button" type=submit name="set_core_keeper" value="$set_core_keeper">"Start Cores "</button>"
echo "</td></tr>"

echo "</form></table>"
#***************************************************************

echo "<form method="POST" action="Save.cgi" $nomount>"
echo "<table align=center>"

#****** 'Wifi: xr819, Wifi: rtl8189es, MMC1 *******
echo "<tr><td align="center"><table class="b2" border="1" cols="4" bgcolor="#ffffff" cellspacing="10">
<caption>$boardmodel</br>"$powerwifi"</caption>"
echo "<tr>"

if [[ "$sdio" == "sdio_wifi" ]]; then
 mod=$(cat $dts | sed -n '/'sdio_wifi@1'/,/\};/p' | sed -n 's/.*compatible.*\"\(.*\)\".*/\1/p')
 mix=$(cat $dts | sed -n '/'wifi-wake'/,/\};/p' | sed -n 's/.*pins.*\"\(.*\)\".*/\1/p')
 ehci=$(cat $dts | sed -n '/'r-wifi-rst'/,/\};/p' | sed -n 's/.*pins.*\"\(.*\)\".*/\1/p')
 ohci=$(cat $dts | sed -n '/'wifi_pwrseq'/,/\};/p' | sed -n 's/.*pinctrl-names.*\"\(.*\)\".*/\1/p')
 if [[ "$mod" == "xradio,xr819" && "$mix" == "PG10" && "$ehci" == "PL7" && "$ohci" == "default" ]]; then
  mod=$(cat $dts | sed -n '/'$wifi'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
  stachec
  echo "
  <td>
  <input type="radio" name="wifi" value="disabled" $sost2 $checked1>OFF
  <input type="radio" name="wifi" value="okay" $sost1 $checked>ON
  </td>
  <td align="center" bgcolor="$bgcolor">"$pownamwifi"</td>
  <td bgcolor="$bgcolor"><b>"$sost2"</b></td>
  <td bgcolor="$bgcolor">"
  if [ "$sost2" == "disabled" ]; then
    echo "$textpownam disabled"
  elif [ "$sost2" == "okay" ]; then
    echo "$textpownam enabled"
  fi
  echo "</td>"
 else
    echo "
    <td align="center" bgcolor="">"missing"</td>
    <td align="left" bgcolor="">No <b>$pownamwifi</b></td>
    <td align="center" bgcolor="">
    <button class="button" type=submit name="overlays" value="wifi">"overlays"</button>
    </td>
    <td align="left" bgcolor="">Add <b>$pownamwifi</b> ?</td>"
 fi
else
  mod=$(cat $dts | sed -n '/'$mmc1'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
  stachec
  echo "
  <td>
  <input type="radio" name="mmc1" value="disabled" $sost2 $checked1>OFF
  <input type="radio" name="mmc1" value="okay" $sost1 $checked>ON
  </td>
  <td align="center" bgcolor="$bgcolor">"$pownamwifi"</td>
  <td bgcolor="$bgcolor"><b>"$sost2"</b></td>
  <td bgcolor="$bgcolor">"
  if [ "$sost2" == "disabled" ]; then
    echo "$textpownam disabled"
  elif [ "$sost2" == "okay" ]; then
    echo "$textpownam enabled"
  fi
  echo "</td>"
fi
echo "
</tr>
</table></td></tr>"
# *** END *** 'Wifi: xr819, Wifi: rtl8189es, MMC1 *******


echo "<tr><td align="center"><table>"

echo "<tr><td align="center"><table class="b2" border="1" bgcolor="#ffffff" cellspacing="2">
<caption>sun8i: Allwinner H2+, H3. General U-boot settings.</caption>"

#**************************i2c0**********************************
echo "
<tr><td>"
mod=$(cat $dts | sed -n '/'$i2c0'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="i2c0" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="i2c0" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"i2c0: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td>"
echo "Gpio: "
cat $dts | sed -n '/'$i2cn0' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>"

#**************************i2c1**********************************
echo "<tr><td>"
mod=$(cat $dts | sed -n '/'$i2c1'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="i2c1" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="i2c1" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"i2c1: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td>"
echo "Gpio: "
cat $dts | sed -n '/'$i2cn1' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>"

#**************************i2c2**********************************
echo "
<tr>
<td>"
mod=$(cat $dts | sed -n '/'$i2c2'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="i2c2" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="i2c2" value="okay" $sost1 $checked>ON
<td align="center" bgcolor="$bgcolor">"i2c2: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td>"
echo "Gpio: "
cat $dts | sed -n '/'$i2cn2' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>"

#**************************uart0**********************************
echo "
<tr>
<td>"
mod=$(cat $dts | sed -n '/'$uart0'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="uart0" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="uart0" value="okay" $sost1 $checked>ON
<td align="center" bgcolor="$bgcolor">"uart0: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td bgcolor="#f0a0f0">"
echo "Gpio: "
cat $dts | sed -n '/'$uartn0' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>"

#**************************uart1**********************************
echo "
<tr>
<td>"
mod=$(cat $dts | sed -n '/'$uart1'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="uart1" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="uart1" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"uart1: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td>"
echo "Gpio: "
cat $dts | sed -n '/'$uartn1' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>"

#**************************uart2**********************************
echo "
<tr>
<td>"
mod=$(cat $dts | sed -n '/'$uart2'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="uart2" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="uart2" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"uart2: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td>"
echo "Gpio: "
cat $dts | sed -n '/'$uartn2' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>"


#**************************spi0**********************************
echo "<tr><td>"
mod=$(cat $dts | sed -n '/'$spi0'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="spi0" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="spi0" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"spi0: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td><td>"
echo "Flash: "
cat $dts | sed -n '/'$spin0' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>"

#*************************spi1***********************************
if [ "$spi1_hide" != "hide" ]; then
echo "<tr><td>"
mod=$(cat $dts | sed -n '/'$spi1'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="spi1" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="spi1" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"spi1: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td><td>"
echo "Gpio: "
cat $dts | sed -n '/'$spin1' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>"
fi

#*************   otg_reg  **********************
echo "<tr><td>"
mod=$(cat $dts | sed -n '/'$otg_reg'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
mod_tipe=$(cat $dts | sed -n '/'$otg_reg'/,/\};/p' | sed -n 's/.*dr_mode.*\"\(.*\)\".*/\1/p')
if [ "$mod_tipe" == "otg" ]; then
checked_o=hidden
otg=""
else
checked_o=""
otg="otg"
fi
if [ "$mod_tipe" == "host" ]; then
checked_h=hidden
host=""
else
checked_h=""
host="host"
fi
if [ "$mod_tipe" == "peripheral" ]; then
checked_p=hidden
per=""
else
checked_p=""
per="per"
fi
echo "
<input type="radio" name="$mod_tipe" value="disabled" $sost2 $checked1>off
<input type="radio" name="otg" value="okay" okay $checked_o title="Enable_otg-driver_for_usb">"$otg"
<input type="radio" name="host" value="okay" okay $checked_h title="Enable_host-driver_for_usb">"$host"
<input type="radio" name="peripheral" value="okay" okay $checked_p title="Enable_peripheral-driver_for_usb.">"$per"
</td>
<td align="center" bgcolor="$bgcolor">"$mod_tipe: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td><td>---</td></tr>"
#************************usb0**********************************



echo "
<tr><td>"
ehci=$(cat $dts | sed -n '/'$ehci0'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
ohci=$(cat $dts | sed -n '/'$ohci0'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stausb
echo "
<input type="radio" name="usb0" value="disabled" $sostusb2 $checusb1>OFF
<input type="radio" name="usb0" value="okay" $sostusb1 $checusb>ON
</td>
<td align="center" bgcolor="$usbcolor">"usb0: "</td><td bgcolor="$usbcolor"><b>"$sostusb2"</b></td><td>---</td></tr>"
#*************************usb1*********************************


echo "
<tr><td>"
ehci=$(cat $dts | sed -n '/'$ehci1'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
ohci=$(cat $dts | sed -n '/'$ohci1'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stausb
echo "
<input type="radio" name="usb1" value="disabled" $sostusb2 $checusb1>OFF
<input type="radio" name="usb1" value="okay" $sostusb1 $checusb>ON
</td>
<td align="center" bgcolor="$usbcolor">"$usb1: "</td><td bgcolor="$usbcolor"><b>"$sostusb2"</b></td><td>"$txt_usb1"</td></tr>"
#************************usb2**********************************


echo "
<tr><td>"
ehci=$(cat $dts | sed -n '/'$ehci2'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
ohci=$(cat $dts | sed -n '/'$ohci2'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stausb
echo "
<input type="radio" name="usb2" value="disabled" $sostusb2 $checusb1>OFF
<input type="radio" name="usb2" value="okay" $sostusb1 $checusb>ON
</td>
<td align="center" bgcolor="$usbcolor">"usb2: "</td><td bgcolor="$usbcolor"><b>"$sostusb2"</b></td><td>---</td></tr>"
#************************usb3*********************************


echo "<tr>
<td>"
ehci=$(cat $dts | sed -n '/'$ehci3'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
ohci=$(cat $dts | sed -n '/'$ohci3'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stausb
echo "
<input type="radio" name="usb3" value="disabled" $sostusb2 $checusb1>OFF
<input type="radio" name="usb3" value="okay" $sostusb1 $checusb>ON
</td>
<td align="center" bgcolor="$usbcolor">"usb3:"</td><td bgcolor="$usbcolor"><b>"$sostusb2"</b></td><td>---</td></tr>"

#*************************ethernet0***************************
echo "<tr><td>"
mod=$(cat $dts | sed -n '/'$ethernet0'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<input type="radio" name="ethernet0" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="ethernet0" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"Ethernet0: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td><td>"
echo "Ethernet0"
#cat $dts | sed -n '/ethernet0 {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td></tr>
</table></table></td></tr>"

#*******************************************************************************


#*******************************************************************************
#*************   Sun8i-DT-overlays  **********************
#**************************************************
echo "<tr><td align="center"><table class="b2" border="1" bgcolor="#ffffff" cellspacing="2">
<caption>Allwinner H2+, H3. Sun8i-DT-overlays.</caption>"

#*************   Analog-audio-codec  **********************
echo "<tr>"
mix=$(cat $dts | sed -n '/'$audcodec'/,/\};/p' | sed -n 's/.*audio-routing.*\"\(.*\)\".*/\1/p')
if [ "$mix" == "Speaker\0LINEOUT\0MIC1\0Mic\0Mic\0MBIAS" ] || [ "$mix" == "Line Out\0LINEOUT\0MIC1\0Mic\0Mic\0MBIAS" ]; then
mod=$(cat $dts | sed -n '/'$audcodec'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<td>
<input type="radio" name="audcodec" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="audcodec" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"Analog-audio-codec: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td align="left">Soc-IN/Out</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor="">Mixer: <b>no line out, no input.</b></td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="codec">"overlays"</button>
</td>
<td align="left" bgcolor="">Add the <b>Mixer</b> ?</td>"
fi
echo "
</tr>"

#*****************  ir  *******************************
echo "<tr>"
mix=$(cat $dts | sed -n '/'$ir'/,/\};/p' | sed -n 's/.*pinctrl-names.*\"\(.*\)\".*/\1/p')
if [ "$mix" == "default" ]; then

mod=$(cat $dts | sed -n '/'$ir'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<td>
<input type="radio" name="ir" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="ir" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"irc: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td align="left">"
echo "Gpio: "
cat $dts | sed -n '/'$irn' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p'
echo "</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor=""><b>IRC</b> contact <b>PL11</b>: not enabled.</td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="ir">"overlays"</button>
</td>
<td align="left" bgcolor="">Add the <b>IRC</b> contact <b>PL11</b> ?</td>"
fi
echo "
</tr>"

#*****************  pwm  *******************************
echo "<tr>"
mod=$(cat $dts | sed -n '/'$pwm'/,/\};/p' | sed -n 's/.*pinctrl-names.*\"\(.*\)\".*/\1/p')
mix=$(cat $dts | sed -n '/'pwm0'/,/\};/p' | sed -n 's/.*pins.*\"\(.*\)\".*/\1/p')
if [[ "$mod" == "default" &&  "$mix" == "PA5" ]]; then
mod=$(cat $dts | sed -n '/'$pwm'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<td>
<input type="radio" name="pwm" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="pwm" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"pwm0: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td align="left" bgcolor="#f0a0f0">"
echo "Gpio: "
cat $dts | sed -n '/pwm0 {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p'
echo " - UART0_RX"
echo "</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor="">PWM: no <b>PA5</b> gpio out. <b>uart0-rx</b></td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="pwm">"overlays"</button>
</td>
<td align="left" bgcolor="">Add <b>pwm-PA5 </b>UART0_RX ?</td>"
fi
echo "
</tr>"

#*************** spdif ******************************************
echo "<tr>"
mod=$(cat $dts | sed -n '/'$spdif'/,/\};/p' | sed -n 's/.*pinctrl-names.*\"\(.*\)\".*/\1/p')
mix=$(cat $dts | sed -n '/'sound_spdif'/,/\};/p' | sed -n 's/.*simple-audio-card,name.*\"\(.*\)\".*/\1/p')
led_s=$(cat $dts | sed -n '/'status_led'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
if [[ "$mod" == "default" && "$mix" == "On-board SPDIF" && "$led_s" == "disabled" ]]; then
mod=$(cat $dts | sed -n '/'$spdif'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<td>
<input type="radio" name="spdif" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="spdif" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"Spdif: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td><td align="left">"
echo "Spdif:"
cat $dts | sed -n '/'$spdifn' {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "$ledspin"
echo "</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor="">SPDIF: no gpio out. <b>red status_led</b></td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="spdif">"overlays"</button>
</td>
<td align="left" bgcolor="">Add <b>SPDIF </b>$ledspin ?</td>"
fi
echo "
</tr>"

#*************** status_led PA17-Red ******************************************
echo "<tr>"
led_s=$(cat $dts | sed -n '/'status_led'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
if [[ "$led_s" == "disabled" || "$led_s" == "okay" ]]; then

mod=$(cat $dts | sed -n '/'status_led'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
stachec
echo "
<td>
<input type="radio" name="status_led" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="status_led" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"$ledspin: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td align="left">"
echo "$ledspin"
echo "</td>"
else
echo "
<td align="center" bgcolor="">"Default"</td>
<td align="left" bgcolor="$bgcolor">Default <b>$ledspin</b></td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="status_led">"overlays"</button>
</td>
<td align="left" bgcolor="$bgcolor">Add <b>$ledspin</b> ?</td>"
fi
echo "
</tr>"

#*************   W1  **********************
echo "<tr>"
mod=$(cat $dts | sed -n '/'$w1' {/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
if [[ "$mod" == "okay" ||  "$mod" == "disabled" ]]; then
stachec
echo "
<td>"
echo "
<input type="radio" name="w1" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="w1" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"w1: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td align="left">"
name_pins_dts="`sed -n '/w1_pins {/,/\};/ s/.*pins = \"\(.*\)\".*/\1/p' $dts`"
case $name_pins_dts in
"PA0") pins0=selected; aa="uart2_tx";;
"PA1") pins1=selected; aa="uart2_rx";;
"PA2") pins2=selected; aa="uart2_rts";;
"PA3") pins3=selected; aa="uart2_cts";;
"PA4") pins4=selected; aa="uart0_tx";;
"PA5") pins5=selected; aa="uart0_rx";;
"PA6") pins6=selected; aa="";;
"PA7") pins7=selected; aa="";;
"PA10") pins10=selected; aa="";;
"PA11") pins11=selected; aa="i2c0_scl";;
"PA12") pins12=selected; aa="i2c0_sda";;
"PA13") pins13=selected; aa="spi1_CS";;
"PA14") pins14=selected; aa="spi1_CLK";;
"PA15") pins15=selected; aa="spi1_MOSI";;
"PA16") pins16=selected; aa="spi1_MISO";;
"PA17") pins17=selected; aa="gpio_Red-Led";;
"PA18") pins18=selected; aa="i2c1_scl";;
"PA19") pins19=selected; aa="i2c1_sda";;
"PG6") pins20=selected; aa="uart1_tx";;
"PG7") pins21=selected; aa="uart1_rx";;
"PL11") pins22=selected; aa="ir-rx-pin";;
esac

modul="w1"; gpios="gpios"; gpios_modul="w1pin"
hex_convert
echo "Gpio: "
select_option

echo $aa
echo "</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor=""><b>W1</b>, free pin contacts.</td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="w1">"overlays"</button>
</td>
<td align="left" bgcolor="">Add the <b>W1</b> ?</td>"
fi
echo "
</tr>"

#*****************  gps-pps  *******************************
echo "<tr>"
mod=$(cat $dts | sed -n '/pps {/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
if [[ "$mod" == "okay" ||  "$mod" == "disabled" ]]; then
stachec
echo "
<td>
<input type="radio" name="pps" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="pps" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"pps-gpio: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td align="left">"

modul="pps"; gpios="\\tgpios"; gpios_modul="gpios_pps"
hex_convert
echo "gpios: "
select_option

modul="pps"; gpios="echo-gpios"; gpios_modul="echo_pps"
hex_convert
echo "echo-gpios: "
select_option

echo "</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor=""><b>pps-gpio</b>, free pin contacts.</td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="pps">"overlays"</button>
</td>
<td align="left" bgcolor="">Add the <b>pps-gpio</b> ?</td>"
fi
echo "
</tr>"

#************   I2C4   **************************
echo "<tr>"
mod=$(cat $dts | sed -n '/'$i2c4'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
if [[ "$mod" == "okay" ||  "$mod" == "disabled" ]]; then
stachec
echo "
<td>
<input type="radio" name="i2c4" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="i2c4" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"I2C4: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td align="left">"

modul="i2c4"; gpios="sda-gpios"; gpios_modul="sda_i2c4"
hex_convert
echo "SDA: "
select_option

modul="i2c4"; gpios="scl-gpios"; gpios_modul="scl_i2c4"
hex_convert
echo ", SCL: "
select_option

echo "</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor=""><b>I2C4</b>, free pin contacts.</td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="i2c4">"overlays"</button>
</td>
<td align="left" bgcolor="">Add the <b>I2C4</b> ?</td>"
fi
echo "
</tr>"


#************   gpio-keys   **************************
echo "<tr>"
mod=$(cat $dts | sed -n '/'$gpio_keys_user'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
if [[ "$mod" == "okay" ||  "$mod" == "disabled" ]]; then
stachec
echo "
<td>
<input type="radio" name="gpio_keys_user" value="disabled" $sost2 $checked1>OFF
<input type="radio" name="gpio_keys_user" value="okay" $sost1 $checked>ON
</td>
<td align="center" bgcolor="$bgcolor">"gpio_keys_user: "</td><td bgcolor="$bgcolor"><b>"$sost2"</b></td>
<td align="left">"
echo "Gpio: "
cat $dts | sed -n '/gpio_keys_pins {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor=""><b>gpio_keys_user</b>, free pin contacts.</td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="gpio_keys_user">"overlays"</button>
</td>
<td align="left" bgcolor="">Add the <b>gpio_keys_user</b> ?</td>"
fi
echo "
</tr>"

#*************    rotary-encoder    ************************
echo "<tr>"
ehci=$(cat $dts | sed -n '/'$rotary_button'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
ohci=$(cat $dts | sed -n '/'$rotary_axis'/,/\};/p' | sed -n 's/.*status.*\"\(.*\)\".*/\1/p')
if [[ "$ehci" == "okay" ||  "$ohci" == "okay" ||  "$ehci" == "disabled" ||  "$ohci" == "disabled" ]]; then
stausb
echo "
<td>
<input type="radio" name="rotary" value="disabled" $sostusb2 $checusb1>OFF
<input type="radio" name="rotary" value="okay" $sostusb1 $checusb>ON
</td>
<td align="center" bgcolor="$usbcolor">"rotary-encoder"</td><td bgcolor="$usbcolor"><b>"$sostusb2"</b></td>
<td align="left">"
echo "button: "
cat $dts | sed -n '/button_pins {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "rotary-encoder: "
cat $dts | sed -n '/rotary_pins {/,/\};/p' | sed -n 's/.*pins = \"\(.*\)\".*/\1/p' | sed 's/\\0\|", "/, /g'
echo "</td>"
else
echo "
<td align="center" bgcolor="">"missing"</td>
<td align="left" bgcolor=""><b>rotary-encoder</b>, free pin contacts.</td>
<td align="center" bgcolor="">
<button class="button" type=submit name="overlays" value="rotary_axis">"overlays"</button>
</td>
<td align="left" bgcolor="">Add the <b>rotary-encoder</b> ?</td>"
fi
echo "
</tr>"

#***********************
echo "</table></td></tr>
</table></td></tr>"
#*******************************************************************************


#*******************************************************************************
cat $dts &> /dev/null
if [ $? != 1 ]; then	# Hide buttons if t-dts is not created.
echo "<table align="center" border=0 width=><tr><td>
<a href=/cgi-bin/modules/boot-config/Save.cgi>
<button class="button" type=submit value="Save" $nomount name=>"Save changes."</button></a>
</td></tr></table>"
echo "</table></form>"
#*****************************************************************
echo "<table align="center" border=0 width=>
<form method="POST" action="conv.cgi"><tr><td>
<a href=/cgi-bin/modules/boot-config/conv.cgi>
<button class="button" $nomount type=submit name="reconv" value="$dtb" title="Compile_of_device_tree.">"Apply changes, and reboot device."</button></a>
</td>
</tr></form></table>"
fi
echo "</body>"
exit 0



